package com.sky.service.impl;

import com.sky.dto.GoodsSalesDTO;
import com.sky.entity.Orders;
import com.sky.mapper.OrderMapper;
import com.sky.mapper.ReportMapper;
import com.sky.mapper.UserMapper;
import com.sky.service.ReportService;
import com.sky.vo.*;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
@Slf4j
public class ReportServiceImpl implements ReportService {
    @Autowired
    private ReportMapper reportMapper;

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private OrderMapper orderMapper;

    @Override
    public TurnoverReportVO getTurnoverReport(LocalDate startDate, LocalDate endDate) {
        List<LocalDate> dates = new ArrayList<>();
        dates.add(startDate);
        LocalDate date = startDate;
        while (!date.equals(endDate)){
            date = date.plusDays(1);
            dates.add(date);
        }

        List<Double> turnoverList = new ArrayList<>();
        for (LocalDate d : dates){
            LocalDateTime start = LocalDateTime.of(d, LocalTime.MIN);
            LocalDateTime end = LocalDateTime.of(d, LocalTime.MAX);
            //select sum(amount) from orders where order_time between startDateTime and endDateTime  and status = 5;
            Map map=new HashMap();
            map.put("begin",start);
            map.put("end",end);
            map.put("status",Orders.COMPLETED);
            Double turnover = reportMapper.getTurnoverByMap(map);
            turnover = turnover==null?0.0:turnover;
            turnoverList.add(turnover);
        }

        TurnoverReportVO turnoverReportVO = TurnoverReportVO.builder()
                .dateList(StringUtils.join(dates, ","))
                .turnoverList(StringUtils.join(turnoverList, ","))
                .build();
        return turnoverReportVO;
    }

    @Override
    public UserReportVO getUserReport(LocalDate begin, LocalDate end) {
        List<LocalDate> dates = new ArrayList<>();
        dates.add(begin);
        while (!begin.equals(end)){
            begin = begin.plusDays(1);
            dates.add(begin);
        }

        //每日新增用户数 select count(id) from user where create_time between startDate_time and endDate_time;
        List<Integer> newUserList = new ArrayList<>();
        //截止到每日总用户数       select count(id) from user where create_time < endDate_time;
        List<Integer> totalUserList = new ArrayList<>();
        for (LocalDate d : dates){
            LocalDateTime startDateTime = LocalDateTime.of(d, LocalTime.MIN);
            LocalDateTime endDateTime = LocalDateTime.of(d, LocalTime.MAX);
            Map map=new HashMap();
            map.put("end",endDateTime);
            Integer totalUser = userMapper.getCountByMap(map);
            map.put("begin",startDateTime);
            Integer newUser = userMapper.getCountByMap(map);
            newUserList.add(newUser);
            totalUserList.add(totalUser);
        }
        return UserReportVO.builder()
                .dateList(StringUtils.join(dates, ","))
                .totalUserList(StringUtils.join(totalUserList, ","))
                .newUserList(StringUtils.join(newUserList, ","))
                .build();
    }

    @Override
    public OrderReportVO getOrderStatistics(LocalDate begin, LocalDate end) {
        List<LocalDate> dates = new ArrayList<>();
        dates.add(begin);
        while (!begin.equals(end)){
            begin = begin.plusDays(1);
            dates.add(begin);
        }
        //每日订单量 select count(id) from orders where order_time between startDate_time and endDate_time
        List<Integer> orderCountList = new ArrayList<>();
        //每日有效订单量 select count(id) from orders where order_time between startDate_time and endDate_time and status = 5
        List<Integer> validOrderCountList = new ArrayList<>();

        for (LocalDate d : dates){
            LocalDateTime startLocalDateTime = LocalDateTime.of(d, LocalTime.MIN);
            LocalDateTime endLocalDateTime = LocalDateTime.of(d, LocalTime.MAX);
            //select sum(amount) from orders where order_time between startDateTime and endDateTime  and status = 5;
            Map map=new HashMap();
            map.put("begin",startLocalDateTime);
            map.put("end",endLocalDateTime);
            Integer orderCount = reportMapper.getCountByMap(map);
            map.put("status",Orders.COMPLETED);
            Integer validOrderCount = reportMapper.getCountByMap(map);
            orderCountList.add(orderCount);
            validOrderCountList.add(validOrderCount);
        }

        //计算时间区间内订单总数,有效订单总数
        Integer totalOrderCount = orderCountList.stream().reduce(Integer::sum).get();
        Integer validTotalOrderCount = validOrderCountList.stream().reduce(Integer::sum).get();
        Double orderCompletionRate = 0.0;
        if (totalOrderCount!= 0) {
            orderCompletionRate = validTotalOrderCount.doubleValue() / totalOrderCount;
        }

        return OrderReportVO.builder()
                .dateList(StringUtils.join(dates, ","))
                .orderCountList(StringUtils.join(orderCountList, ","))
                .validOrderCountList(StringUtils.join(validOrderCountList, ","))
                .totalOrderCount(totalOrderCount)
                .validOrderCount(validTotalOrderCount)
                .orderCompletionRate(orderCompletionRate)
                .build();
    }

    @Override
    public SalesTop10ReportVO getSalesTop10(LocalDate begin, LocalDate end) {

        LocalDateTime beginLocalDateTime = LocalDateTime.of(begin, LocalTime.MIN);
        LocalDateTime endLocalDateTime = LocalDateTime.of(end, LocalTime.MAX);

        //select od.name,sum(od.number) from orders o join order_detail od on o.id = od.order_id
        // where o.order_time between startDate_time and endDate_time and o.status = 5
        // group by od.name order by sum(od.number) desc;
        List<GoodsSalesDTO> salesTop10List = orderMapper.getSalesTop10(beginLocalDateTime, endLocalDateTime);
        List<String> nameList = salesTop10List.stream().map(GoodsSalesDTO::getName).collect(Collectors.toList());
        List<Integer> numberList = salesTop10List.stream().map(GoodsSalesDTO::getNumber).collect(Collectors.toList());


        return SalesTop10ReportVO.builder()
                .nameList(StringUtils.join(nameList, ","))
                .numberList(StringUtils.join(numberList, ","))
                .build();
    }
}
